clearvars -except Globaloption

if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

warning off

data = readtable('../../data/main_VAR/UK_full.xlsx');
date = data.Year;

startdatenum = 1729;
enddatenum = 2020;

startdate = find(date == startdatenum);
enddate = find(date == enddatenum);
T_post = length(date(startdate:enddate));

Tstart = startdate;
Tend = enddate;
T = Tend - Tstart + 1;

date = date(startdate:enddate);

data = data((data.Year >= startdatenum - 1), :);
data = data((data.Year <= enddatenum), :);

taxrevgdp = data.revtogdp(2:end); % government tax revenue to GDP ratio
taxrevgdp0 = data.revtogdp(1); % government tax revenue to GDP ratio
spendgdp = data.spendingtogdp(2:end); % government spending before interest exdp. to GDP ratio
spendgdp0 = data.spendingtogdp(1); % government spending before interest exp. to GDP ratio
surplusgdp = taxrevgdp - spendgdp; % primary surplus to gdp
inflation = data.inflation;

save MAT\uk_fullsample.mat;
%%

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on
ylim([-60 90])
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
hold on
patch([date(find(date == 1939)) date(find(date == 1939)) date(find(date == 1945)) date(find(date == 1945))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

patch([date(find(date == 1854)) date(find(date == 1854)) date(find(date == 1856)) date(find(date == 1856))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

patch([date(find(date == 1899)) date(find(date == 1899)) date(find(date == 1902)) date(find(date == 1902))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1775)) date(find(date == 1775)) date(find(date == 1783)) date(find(date == 1783))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1756)) date(find(date == 1756)) date(find(date == 1763)) date(find(date == 1763))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1793)) date(find(date == 1793)) date(find(date == 1802)) date(find(date == 1802))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1812)) date(find(date == 1812)) date(find(date == 1814)) date(find(date == 1814))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
date2 = [date; 2021; 2022; 2023; 2024; 2025];
patch([date2(find(date2 == 2020)) date2(find(date2 == 2020)) date2(find(date2 == 2022)) date2(find(date2 == 2022))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

ha = plot(date(find(date == 1729):end), 100 * surplusgdp(find(date == 1729):end), 'black', 'LineWidth', 1.5);
hold on
hb = plot(date(find(date == 1729):end), 100 * taxrevgdp(find(date == 1729):end), 'red--', 'LineWidth', 2);
hold on
hc = plot(date(find(date == 1729):end), 100 * spendgdp(find(date == 1729):end), 'blue-.', 'LineWidth', 2);
xline(1946)
plot(date(find(date == 1729):find(date == 1946)), mean(surplusgdp(find(date == 1729):find(date == 1946))) * ones(1, find(date == 1946) - find(date == 1729) + 1))
hold on
plot(date(find(date == 1946):end), nanmean(surplusgdp(find(date == 1946):end)) * ones(1, length(surplusgdp(find(date == 1946):end))))

xlim([1729 2027])
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');
grid on
ylabel('% of GDP')
gtext('Seven Years War')
gtext('American')
gtext('Revolutionary')
gtext('War')
gtext('French')
gtext('Napoleonic')
gtext('Wars')
gtext('War of ')
gtext('Sixth Coalition')
gtext('Crimean War')
gtext('Boer War')
gtext('WWI')
gtext('WWII')
gtext('Covid')

if Globaloption.savefig == 1

    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/UK/UK_surpluses.pdf', '-dpdf', '-fillpage')

end

%% business cycle

bc_data = readtable('../../data/supplement/UK_BC.xlsx');
peak = bc_data.GDP;
peak = peak(find(peak >= 1729):72);

trough = bc_data.Var2;
trough = trough(find(trough > 1729):72);

clear bc_data
save 'MAT/uk_bc.mat'

load 'MAT/uk_bc.mat'
f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on
ylim([-50 80])

for tt = 1:length(peak)
    patch([date(find(date == peak(tt))) date(find(date == peak(tt))) date(find(date == trough(tt))) date(find(date == trough(tt)))], [min(ylim + 0.1) max(ylim - 0.1) max(ylim - 0.1) min(ylim + 0.1)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
    hold on
end

ha = plot(date(find(date == 1729):end), 100 * surplusgdp(find(date == 1729):end), 'black', 'LineWidth', 2);
hold on
hb = plot(date(find(date == 1729):end), 100 * taxrevgdp(find(date == 1729):end), 'red--', 'LineWidth', 2);
hold on
hc = plot(date(find(date == 1729):end), 100 * spendgdp(find(date == 1729):end), 'blue-.', 'LineWidth', 2);
hg = xline(1946);
hd = plot(date(find(date == 1729):find(date == 1946)), mean(surplusgdp(find(date == 1729):find(date == 1946))) * ones(1, find(date == 1946) - find(date == 1729) + 1))
hold on
he = plot(date(find(date == 1946):end), nanmean(surplusgdp(find(date == 1946):end)) * ones(1, length(surplusgdp(find(date == 1946):end))))
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');
xlim([1729 2020])
grid on
ylabel('% of GDP')

if Globaloption.savefig == 1
    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/UK/UK_surpluses_recession.pdf', '-dpdf', '-fillpage');
end

%% debt to gdp ratio

gdebt = data.debttogdplevel(2:end); % 1729 - 2020 marketable

uk_debtgdp = data.debttogdp_mil(2:end);

f = figure('units', 'normalized', 'outerposition', [0 0 0.5 0.5]);
box on
ylim([0 3])
patch([date(find(date == 1914)) date(find(date == 1914)) date(find(date == 1918)) date(find(date == 1918))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
hold on
patch([date(find(date == 1939)) date(find(date == 1939)) date(find(date == 1945)) date(find(date == 1945))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

patch([date(find(date == 1854)) date(find(date == 1854)) date(find(date == 1856)) date(find(date == 1856))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

patch([date(find(date == 1899)) date(find(date == 1899)) date(find(date == 1902)) date(find(date == 1902))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1775)) date(find(date == 1775)) date(find(date == 1783)) date(find(date == 1783))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1756)) date(find(date == 1756)) date(find(date == 1763)) date(find(date == 1763))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1793)) date(find(date == 1793)) date(find(date == 1802)) date(find(date == 1802))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date(find(date == 1812)) date(find(date == 1812)) date(find(date == 1814)) date(find(date == 1814))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')
patch([date2(find(date2 == 2020)) date2(find(date2 == 2020)) date2(find(date2 == 2022)) date2(find(date2 == 2022))], [min(ylim) max(ylim) max(ylim) min(ylim)], [0.8 0.8 0.8], 'EdgeColor', [1, 1, 1], 'LineStyle', 'none')

ha = plot(date(find(date == 1729):end), gdebt(find(date == 1729):end), 'black', 'LineWidth', 1.5)

hb = plot(date(find(date == 1729):end), uk_debtgdp(find(date == 1729):end), 'black--', 'LineWidth', 1.5)

xlim([1729 2025])
ylabel('% of GDP')
grid on
hleglines = [ha hb];
legend(hleglines, 'Marketable Debt (Ellison-Scott)', 'Marketable +NonMarketable (Bank of England)', 'Location', 'Southwest')

gtext('Seven Years War')
gtext('American')
gtext('Revolutionary')
gtext('War')
gtext('French')
gtext('Napoleonic Wars')
gtext('War of ')
gtext('Sixth Coalition')
gtext('Crimean War')
gtext('Boer War')
gtext('WWI')
gtext('WWII')
gtext('Covid')

if Globaloption.savefig == 1
    set(gcf, 'PaperUnits', 'inches', 'Units', 'inches');
    f.PaperSize = [10 5];
    print('../../results/figures_main/UK/UK_debtgdp.pdf', '-dpdf', '-fillpage');
end
